import $ from "jquery";
import {url} from "../../util/config";
import {error as exception} from "../../util/responseStatus";
import {message} from "antd";
import login from "../login";
function getNewLogin() {
    return getRole()
}
export default function getRole() {
    let roles = [];
    let token = localStorage.getItem("token")
    $.ajax({
        async:false,
        headers: {
            'Accept': "application/json",
             'Authorization': token
        },
        url: url.api_url+`Role/GetRole`,
        type: 'GET',
        success: function (result) {
            if(result.result=="Token過期請重新登錄"){
                login(localStorage.getItem("account"), localStorage.getItem("password"));
                roles = getNewLogin()
            }else {
                if(result.result=="该用户无权限查看！"){
                    return
                }
                roles = result.result.map(item=>{
                        if(item.ROLE_TYPE=="user"){
                            item.ROLE_TYPE="用户"
                        }else item.ROLE_TYPE = "管理员"
                        return (
                            {
                                key:item.ROLE_ID,
                                name:item.ROLE_NAME,
                                type:item.ROLE_TYPE,
                                desc:item.ROLE_DESC,
                                createUser:item.CREATE_USER,
                                createTime:item.CREATE_DATE,
                                updateDate:item.UPDATE_DATE,
                                updateUser:item.UPDATE_USER
                                //         已經註銷     sequence:item.ROLE_ORDER
                            }
                        )
                    }
                )

                sessionStorage.setItem("roles",JSON.stringify(roles))
            }
        },
        error:function (error) {
            message.error("请求异常")
        }
    })
    return roles;

}
